#version 430
uniform mat4 u_view;
uniform mat4 u_proj;
uniform mat4 u_model;
uniform sampler2D u_heightmap;
uniform float u_scale;
uniform float u_offset;
uniform vec3 u_gnd2sun;//this is normalized, from ground point to sun.
uniform vec4 u_clipplane;
layout(location = 0) in vec3 position;
layout(location = 1) in vec2 uv;

const vec3 colorarray[9]=vec3[9](
	vec3(0.17, 0.26, 0.00),
	vec3(0.42, 0.60, 0.53),
	vec3(0.47, 0.67, 0.59),
	vec3(0.58, 0.73, 0.63),
	vec3(0.82, 0.78, 0.70),
	vec3(0.84, 0.73, 0.64),
	vec3(0.84, 0.79, 0.76),
	vec3(0.87, 0.87, 0.88),
	vec3(0.96, 0.96, 0.96)
);
const float vstep = 1.0 / 9.0;
out vec4 vcolor;
out vec2 vuv;

void main()
{
	vec3 posi2 = position;
	float heightval = texture(u_heightmap, uv).r ;
	int ci = min(8, int(heightval / vstep));
	int ci2 = min(8, ci + 1);
	float weight =  ( heightval - ci*vstep ) / vstep ;
	posi2.y = (heightval + u_offset) * u_scale;

	vec4 worldposition = u_model * vec4(posi2, 1.0);

	gl_Position = u_proj * u_view * worldposition;
	vcolor = vec4( mix( colorarray[ci],colorarray[ci2],weight) , 1.0) ;
	//vcolor = vec4(vec3(posi2.y), 1.0);
	//vcolor = vec4(uv.x, uv.y , 0.0, 1.0);
	vuv = uv;
	//clip
	gl_ClipDistance[0] = dot(worldposition, u_clipplane);
}